package com.kg.sdk.view;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.kpgamesdk.R;

/**
 * Created by Administrator on 2017-07-14.
 */
//com.common.ui.load.LoadInputLayout
@SuppressLint("NewApi") public class LoadInputLayout extends RelativeLayout implements View.OnClickListener ,TextWatcher {
    protected AutoFitEditText etInput;
    protected AutoFitTextView  btnRight;
    private Drawable btnDrawable;
    private int inputStyle = TEXT;
    public final static int TEXT = 0;
    public final static int PASSOWRD = 1;
    public final static int CHECK = 2;
    public final static int NUMBER = 3;
    public final static int PHONE = 4;
    private static final int BTNId = 1000;
    private static final int LEFTID = 1001;
    protected static final int DEFAULT_SIZE = 15;
    private boolean isShowPassword = false;
    private int defaultSize;
    private int margin;
    private View rightView;
    public LoadInputLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomLoad);
        defaultSize = (int)context.getResources().getDimension(R.dimen.load_btn_default_size);
        margin = (int)context.getResources().getDimension(R.dimen.margin);
        inputStyle =  ta.getInt(R.styleable.CustomLoad_inputStyle,TEXT);
        if(inputStyle == TEXT || inputStyle == PASSOWRD || inputStyle == PHONE){
            setRightImg(context,ta);
        }else if(inputStyle == CHECK){
            setRightBtn(context,ta);
        }
        boolean isShow =  ta.getBoolean(R.styleable.CustomLoad_showLeftImg,true);
        if(isShow){
            setLeftImage(context,ta);
        }
        setEditInput(context,ta);
        /*
        Drawable leftDrawable = ta.getDrawable(R.styleable.CustomLoad_leftDrawable);
        if(leftDrawable!=null){
            leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight());//必须设置图片大小，否则不显示
            etInput.setCompoundDrawables(leftDrawable,null,null, null);
           // etInput.setCompoundDrawablePadding(leftDrawablePadding);
        }*/
        Log.e("hit"," inputStyle is "+inputStyle);
        this.addView(etInput);
        // this.setBackgroundResource(R.drawable.load_input_bk);
        setInputStyle();
        ta.recycle();
    }

    public void setEditText(String text){
        etInput.setText(text);
    }
    protected void setEditInput(Context context, TypedArray ta){
        etInput = new AutoFitEditText(context);
        LayoutParams etParams = new LayoutParams
                (ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        if(inputStyle != NUMBER){
            etParams.addRule(RelativeLayout.LEFT_OF,BTNId);
        }
        int verticalCenterRule = RelativeLayout.CENTER_VERTICAL;
        etParams.addRule(RelativeLayout.RIGHT_OF,LEFTID);
        etParams.addRule(verticalCenterRule,RelativeLayout.TRUE);
        String hint =  ta.getString(R.styleable.CustomLoad_hintText);
        int textSize = (int)ta.getDimension(R.styleable.CustomLoad_inputTextSize,DEFAULT_SIZE);
        Log.e("hit"," hint is "+hint);
        if(hint != null){
            etInput.setHint(hint);
        }
        int hintColor = ta.getColor(R.styleable.CustomLoad_inputHintTextColor, 0xffC5C8CE );
        etInput.setBackgroundDrawable(null);
        etInput.setSingleLine(true);
        etInput.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
        etInput.setMaxTextSize((int)etInput.getTextSize());
        etInput.setTextColor(0xff515A6E);
        etInput.setMinTextSize((int)etInput.getTextSize());
        etInput.setHintTextColor(hintColor);
        //   etInput.setPadding(margin*2,0,0,0);
        etInput.setLayoutParams(etParams);
        etInput.addTextChangedListener(this);
    }
    private void setLeftImage(Context context, TypedArray ta) {
        ImageView leftImg = new ImageView(context);
        LayoutParams btnParams = new LayoutParams
                (ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        leftImg.setId(LEFTID);
        int verticalCenterRule = RelativeLayout.CENTER_VERTICAL;
        int leftRule = RelativeLayout.ALIGN_PARENT_LEFT;
        btnParams.setMargins(0,0,margin,0);
        btnParams.addRule(leftRule,RelativeLayout.TRUE);
        btnParams.addRule(verticalCenterRule ,RelativeLayout.TRUE);
        Drawable leftDrawable = ta.getDrawable(R.styleable.CustomLoad_leftDrawable);
        if(leftDrawable != null){
            leftImg.setImageDrawable(leftDrawable);
        }
        leftImg.setLayoutParams(btnParams);
        this.addView(leftImg);
    }

    private void setRightImg(Context context,  TypedArray ta){
        View view  = LayoutInflater.from(context).inflate(R.layout.lib_close,this,true);
        rightView = findViewById(R.id.frClose);
        LayoutParams btnParams = (LayoutParams) rightView.getLayoutParams();
        rightView.setId(BTNId);
        defaultSize = (int)context.getResources().getDimension(R.dimen.load_btn_default_size1);
        int btnWidth = (int)ta.getDimension(R.styleable.CustomLoad_btnWidth,defaultSize);
        int btnHeight = (int)ta.getDimension(R.styleable.CustomLoad_btnHeight,defaultSize);
        if(btnHeight != 0 ){
            btnParams.height = btnHeight;
        }
        if(btnWidth != 0){
            btnParams.width = btnWidth;
        }
        rightView.setOnClickListener(this);
        ImageView imageView = (ImageView)findViewById(R.id.imgClose);
        btnDrawable = ta.getDrawable(R.styleable.CustomLoad_btnDrawable);
        if(btnDrawable != null){
            imageView.setBackgroundDrawable(btnDrawable);
        }

    }
    private void setRightBtn(Context context,  TypedArray ta) {
        btnRight = new AutoFitTextView(context);
        btnRight.setGravity(Gravity.CENTER);
        LayoutParams btnParams = new LayoutParams
                (ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        btnRight.setId(BTNId);
        int verticalCenterRule = RelativeLayout.CENTER_VERTICAL;
        int rightRule = RelativeLayout.ALIGN_PARENT_RIGHT;
        btnParams.addRule(rightRule,RelativeLayout.TRUE);
        btnParams.addRule(verticalCenterRule ,RelativeLayout.TRUE);
        btnDrawable = ta.getDrawable(R.styleable.CustomLoad_btnDrawable);
        int btnTextColor = ta.getColor(R.styleable.CustomLoad_btnTextColor,0xff00A0F3);
        btnRight.setTextColor(btnTextColor);
        btnRight.setBackgroundDrawable(null);
        if(btnDrawable != null){
          //  btnRight.setBackgroundDrawable(btnDrawable);
        }
        int btnWidth = (int)ta.getDimension(R.styleable.CustomLoad_btnWidth,defaultSize);
        int btnHeight = (int)ta.getDimension(R.styleable.CustomLoad_btnHeight,defaultSize);
        if(btnHeight != 0 ){
            btnParams.height = btnHeight;
        }
        if(btnWidth != 0){
            btnParams.width = btnWidth;
        }
        btnParams.leftMargin = margin;
        btnParams.rightMargin = margin;
        int textSize = (int)ta.getDimension(R.styleable.CustomLoad_btnTextSize,DEFAULT_SIZE);
        btnRight.setMaxTextSize(textSize);
        btnRight.setTextSize(TypedValue.COMPLEX_UNIT_PX,textSize);
        btnRight.setLayoutParams(btnParams);
        btnRight.setPadding(margin,0,margin,0);
        String text = ta.getString(R.styleable.CustomLoad_btnText);
        Log.e("hit"," text is "+text+" btnHeight is "+btnHeight+" btnWidth is "+btnWidth);
        if(text != null){
            btnRight.setText(text);
        }
        this.addView(btnRight);
        btnRight.setOnClickListener(this);
    }

    public void setBtnRightBk(int bk){
        if(btnRight  != null){
            btnRight.setBackgroundResource(bk);
        }
    }

    public void setBtnRightTextColor(int textColor){
        if(btnRight  != null){
            btnRight.setTextColor(textColor);
        }
    }
    @SuppressLint("NewApi") @TargetApi(Build.VERSION_CODES.CUPCAKE) private void setInputStyle(){
        switch (inputStyle){
            case TEXT:
                etInput.setInputType(InputType.TYPE_CLASS_TEXT);

                // btnRight.setVisibility(View.GONE);
                setRighShow(false);
                break;
            case PHONE:
                etInput.setInputType(InputType.TYPE_CLASS_PHONE);

                // btnRight.setVisibility(View.GONE);
                setRighShow(false);
                break;
            case PASSOWRD:
                etInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                //   btnRight.setVisibility(View.GONE);
                break;
            case CHECK:
                etInput.setInputType(InputType.TYPE_CLASS_TEXT);
                break;
            case NUMBER:
                etInput.setInputType(InputType.TYPE_CLASS_NUMBER);
                break;
        }
    }
    public LoadInputLayout(Context context) {
        super(context);
    }

    public void clearInputText(){
        etInput.setText("");
    }

    @Override
    public void onClick(View view) {
        switch (inputStyle){
            case TEXT:
                clearInputText();
                break;
            case PHONE:
                clearInputText();
                break;
            case PASSOWRD:
                setPasswordShow();
                break;
            case CHECK:
                break;
        }
    }

    @SuppressLint("NewApi") public void setPasswordShow(){
        if(!isShowPassword){
            isShowPassword = true;
            etInput.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }else{
            isShowPassword = false;
            etInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
    }
    public String getInputText(){
        if(etInput != null){
            return etInput.getText().toString();
        }
        return "";
    }

    public void setBtnRightClickListener(OnClickListener listener){
        if(btnRight != null){
            btnRight.setOnClickListener(listener);
        }
        if(rightView != null){
            rightView.setOnClickListener(this);
        }
    }

    private void setRighShow(boolean is){

        if(btnRight != null){
            if(is){
                btnRight.setVisibility(View.VISIBLE);
            }else{
                btnRight.setVisibility(View.GONE);
            }

        }
        if(rightView != null){
            if(is){
                rightView.setVisibility(View.VISIBLE);
            }else{
                rightView.setVisibility(View.GONE);
            }
        }
    }
    public void setBtnRightText(String text){
        if(btnRight != null){
            btnRight.setText(text);
        }
    }

    public void setBtnRightEnable(boolean is){
        if(btnRight != null){
            btnRight.setEnabled(is);
        }
        if(rightView != null){
            rightView.setEnabled(is);
        }
    }
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        if(inputStyle == TEXT || inputStyle == PHONE){
            int len = editable.length();
            if(len > 0){
                setRighShow(true);
            }else{
                //   btnRight.setVisibility(View.GONE);
                setRighShow(false);
            }
        }
    }
}

